package com.itboy.mall.pms.attr.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import com.itboy.mall.pms.attrattrgroup.domain.MallAttrAttrGroup;
import com.itboy.mall.pms.attrattrgroup.service.IMallAttrAttrGroupService;
import com.itboy.mall.pms.attrattrgroup.vo.MallAttrAttrGroupVo;
import com.itboy.mall.pms.category.domain.MallCategory;
import com.itboy.mall.pms.category.service.IMallCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.pms.attr.bo.MallAttrAddBo;
import com.itboy.mall.pms.attr.bo.MallAttrQueryBo;
import com.itboy.mall.pms.attr.bo.MallAttrEditBo;
import com.itboy.mall.pms.attr.domain.MallAttr;
import com.itboy.mall.pms.attr.mapper.MallAttrMapper;
import com.itboy.mall.pms.attr.vo.MallAttrVo;
import com.itboy.mall.pms.attr.service.IMallAttrService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

import static com.itboy.mall.pms.attr.constant.AttrEnum.MALL_ATTR_SPEC;
import static com.itboy.mall.pms.attr.constant.AttrEnum.MALL_ATTR_SELL;

/**
 * 商品属性Service业务层处理
 *
 * @author itboy
 * @date 2021-07-24
 */
@Service
public class MallAttrServiceImpl extends ServicePlusImpl<MallAttrMapper, MallAttr> implements IMallAttrService {

	@Autowired
	private IMallCategoryService iMallCategoryService;

	@Autowired
	private IMallAttrAttrGroupService iMallAttrAttrGroupService;

	@Autowired
	private IMallAttrService iMallAttrService;

	@Override
	public TableDataInfo<MallAttrVo> listByNoAttrId(MallAttrQueryBo bo) {
		//查询关联表
		LambdaQueryWrapper<MallAttrAttrGroup> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(MallAttrAttrGroup::getAttrGroupId,bo.getAttrGroupId());
		List<MallAttrAttrGroup>  mallAttrAttrGroupList= iMallAttrAttrGroupService.list(queryWrapper);
		List<Long> attrIdList = mallAttrAttrGroupList.stream().map(e -> e.getAttrId()).collect(Collectors.toList());

		//查询属性表

		LambdaQueryWrapper<MallAttr> lqw = Wrappers.lambdaQuery();
		lqw.notIn(CollectionUtil.isNotEmpty(attrIdList),MallAttr::getAttrId,attrIdList);
		lqw.eq(MallAttr::getCatalogId,bo.getCatalogId());
		lqw.like(StrUtil.isNotBlank(bo.getAttrName()), MallAttr::getAttrName, bo.getAttrName());
		PagePlus<MallAttr, MallAttrVo> result= pageVo(PageUtils.buildPagePlus(), lqw, MallAttrVo.class);

		return PageUtils.buildDataInfo(result);
	}




    @Override
    public MallAttrVo queryById(Long attrId){
		MallAttrVo mallAttrVo = getVoById(attrId, MallAttrVo.class);
		return mallAttrVo;
    }

    @Override
    public TableDataInfo<MallAttrVo> queryPageList(MallAttrQueryBo bo) {
        PagePlus<MallAttr, MallAttrVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallAttrVo.class);
		List<MallAttrVo> recordsVo = result.getRecordsVo();
		//分类id
		List<Long> collect = recordsVo.stream().map(e -> e.getCatalogId()).collect(Collectors.toList());
		if(CollectionUtil.isNotEmpty(collect)){
			LambdaQueryWrapper<MallCategory> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.in(MallCategory::getId,collect);
			List<MallCategory> categoryList = iMallCategoryService.list(queryWrapper);
			//设置分类名称
			recordsVo.forEach(e->{
				MallCategory mallCategory = categoryList.stream().filter(t -> t.getId().equals(e.getCatalogId())).findFirst().orElse(null);
				if(ObjectUtil.isNotNull(mallCategory)){
					e.setCatalogName(mallCategory.getName());
				}
			});
		}
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallAttrVo> queryList(MallAttrQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallAttrVo.class);
    }

    private LambdaQueryWrapper<MallAttr> buildQueryWrapper(MallAttrQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallAttr> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getAttrName()), MallAttr::getAttrName, bo.getAttrName());
        lqw.eq(bo.getAttrType() != null, MallAttr::getAttrType, bo.getAttrType());
        lqw.eq(bo.getEnable() != null, MallAttr::getEnable, bo.getEnable());
        lqw.eq(bo.getCatalogId() != null, MallAttr::getCatalogId, bo.getCatalogId());
        lqw.eq(bo.getShowDesc() != null, MallAttr::getShowDesc, bo.getShowDesc());
        return lqw;
    }

    @Override
	@Transactional(rollbackFor = Exception.class)
    public Boolean insertByAddBo(MallAttrAddBo bo) {
        MallAttr add = BeanUtil.toBean(bo, MallAttr.class);
        if(bo.getAttrType()==MALL_ATTR_SPEC.getValue()){
        	//商品属性
			add.setAttrType(MALL_ATTR_SPEC.getValue());
		}else{
        	//销售属性
			add.setAttrType(MALL_ATTR_SELL.getValue());
		}

		boolean save = save(add);
		//保存关联属性
		if(bo.getAttrGroupId()!=null){
			MallAttrAttrGroup mallAttrAttrGroup = new MallAttrAttrGroup();
			mallAttrAttrGroup.setAttrId(add.getAttrId());
			mallAttrAttrGroup.setAttrGroupId(bo.getAttrGroupId());
			iMallAttrAttrGroupService.save(mallAttrAttrGroup);
		}
        return save;
    }

    @Override
	@Transactional(rollbackFor = Exception.class)
    public Boolean updateByEditBo(MallAttrEditBo bo) {
        MallAttr update = BeanUtil.toBean(bo, MallAttr.class);
		if(bo.getAttrType()==MALL_ATTR_SPEC.getValue()){
			update.setAttrType(MALL_ATTR_SPEC.getValue());
		}else{
			update.setAttrType(MALL_ATTR_SELL.getValue());
		}
		boolean b = updateById(update);
		//保存关联属性
		if(bo.getAttrGroupId()!=null){
			MallAttrAttrGroup mallAttrAttrGroup = new MallAttrAttrGroup();
			mallAttrAttrGroup.setAttrId(update.getAttrId());
			mallAttrAttrGroup.setAttrGroupId(bo.getAttrGroupId());
			LambdaQueryWrapper<MallAttrAttrGroup> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq(MallAttrAttrGroup::getAttrId,update.getAttrId());
			iMallAttrAttrGroupService.saveOrUpdate(mallAttrAttrGroup,queryWrapper);
		}
        return b;
    }



    @Override
	@Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		//删除关联属性
		LambdaQueryWrapper<MallAttrAttrGroup> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.in(MallAttrAttrGroup::getAttrId,ids);
		iMallAttrAttrGroupService.remove(queryWrapper);
        return removeByIds(ids);
    }

	@Override
	public List<MallAttrVo> getListByGroupId(Long attrGroupId,Integer attrType) {

		LambdaQueryWrapper<MallAttrAttrGroup> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(MallAttrAttrGroup::getAttrGroupId,attrGroupId);
		List<MallAttrAttrGroupVo> mallAttrAttrGroupVoList = iMallAttrAttrGroupService.listVo(wrapper, MallAttrAttrGroupVo.class);
		if(CollectionUtil.isNotEmpty(mallAttrAttrGroupVoList)){
			List<Long> attrIdList = mallAttrAttrGroupVoList.stream().map(e -> e.getAttrId()).collect(Collectors.toList());
			LambdaQueryWrapper<MallAttr> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.in(MallAttr::getAttrId,attrIdList);
			queryWrapper.eq(attrType!=null,MallAttr::getAttrType,attrType);
			List<MallAttrVo> mallAttrVoList = iMallAttrService.listVo(queryWrapper, MallAttrVo.class);
			return mallAttrVoList;
		}
		return null;
	}

	@Override
	public List<MallAttr> getListByCatelogId(Long catalogId) {
		LambdaQueryWrapper<MallAttr> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(MallAttr::getCatalogId,catalogId);
		wrapper.eq(MallAttr::getAttrType,MALL_ATTR_SELL.getValue());
		List<MallAttr> mallAttrVoList = iMallAttrService.list(wrapper);
		return mallAttrVoList;
	}
}
